package swingtest;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

class J_ActionListener implements ActionListener{
	
	private int clickTiems= 0;
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JButton button = (JButton)e.getSource();
		clickTiems ++;
		button.setText("点击了"+clickTiems+"次");
	}
	
}

public class J_Button {

	public static void main(String[] args) {
		JFrame app = new JFrame("J_Button");
		app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		app.setSize(600, 400);
		app.setLocationByPlatform(true);

		Container container = app.getContentPane();
		container.setLayout(new FlowLayout(FlowLayout.LEFT));
		container.setBackground(new Color(24, 244, 20));

		ActionListener actionListener = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("点击了" + e.getActionCommand());
			}
		};
		ImageIcon icon1 = new ImageIcon("ic.png");
		ImageIcon icon2 = new ImageIcon("sending.gif");
		JButton button1 = new JButton("左", icon1);
		JButton button2 = new JButton("中");
		JButton button3 = new JButton("右", icon2);
		JButton button4 = new JButton("请点击");
		JButton button5 = new JButton("请点击");
		button1.setToolTipText("请点击左键");
		button2.setToolTipText("请点击中键");
		button3.setToolTipText("请点击右键");
		button1.addActionListener(actionListener);
		button2.addActionListener(actionListener);
		button3.addActionListener(actionListener);
		button4.addActionListener(new J_ActionListener());
		button5.addActionListener(new ActionListener() {
			int clickTimes = 0;
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				clickTimes++;
				JButton button = (JButton)e.getSource();
				button.setText("点击了"+clickTimes+"次");
			}
		});
		container.add(button1);
		container.add(button2);
		container.add(button3);
		container.add(button4);
		container.add(button5);

		JCheckBox checkBox1 = new JCheckBox("左");
		JCheckBox checkBox2 = new JCheckBox("右", true);
		checkBox1.addActionListener(actionListener);
		checkBox2.addActionListener(actionListener);
		container.add(checkBox1);
		container.add(checkBox2);

		JRadioButton radioButton1 = new JRadioButton("左");
		JRadioButton radioButton2 = new JRadioButton("右", true);
		radioButton1.addActionListener(actionListener);
		radioButton2.addActionListener(actionListener);
		ButtonGroup bGroup = new ButtonGroup();
		bGroup.add(radioButton1);
		bGroup.add(radioButton2);
		container.add(radioButton1);
		container.add(radioButton2);

		app.setVisible(true);

	}

}
